// components/FuzzyQuery/FuzzyQuery.js

import {

  ApiPhone
} from "../../api/index"
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    // UserList: {
    //   type: Array,
    //   value: []
    // },
    UserBackups: {
      type: Array,
      value: []
    },
    index: {
      type: String
    },
    roleName1: {
      type: String,
      value: ''
    },
    textValue: {
      type: String,
      value: ''
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    textValue: '',
    defaultIndex: 0,
    UserSwitch: false,
    searchKey: '',
    UserList: [],
    UserBackups: [],
  },

  /**
   * 组件的方法列表
   */
  methods: {
    UserShutdown() {
      this.setData({
        UserSwitch: false
      })
      this.resetColumns()
    },
    openSwitch() {
      this.setData({
        UserSwitch: true,
        searchKey: '',
        UserBackups: this.data.UserList
      })
    },
    resetColumns() {
      this.setData({
        searchKey: '',
        UserList: this.data.UserBackups
      })
    },
    //点击确定搜索时触发事件
    onSearch(e) {
      // console.log(e.detail)
      // console.log(this.data.UserBackups, 'this.data.UserBackups');
      let query = {}
      if (e.detail) {
        query.phone = e.detail
      } else {
        query = {}
      }

      ApiPhone(query).then(res => {
        if (res.data && res.data.length > 0) {
          this.setData({
            UserList: res.data
          })
        } else {

          this.data.UserList[this.data.index] = {
            phone: e.detail,
            name: '',
            age: '',
            unit: ''
          }
          this.setData({
            cognizance: e.detail,
            UserList: this.data.UserList
          })
        }

      })
      // let row = [];
      // if (this.data.searchKey.length > 0) {
      //   this.data.UserBackups.filter(
      //     (e) => {
      //       if (e.projectName.indexOf(this.data.searchKey.toLowerCase()) != -1) {
      //         row.push(e)
      //       }
      //     })
      // } else {
      //   row = this.data.UserBackups
      // }

    },

    onClose() {
      this.setData({
        UserSwitch: false
      })
    },
    UserIdentification(event) {
      // console.log(event, 'event');
      // let id = event.detail.value.id;
      // let index = event.detail.index
      // let UserBackups = this.data.UserBackups;
      this.setData({
        cognizance: event.detail.value.phone,
        // textValue: this.data.UserBackups[index].projectName,
        UserSwitch: false,
        // UserList: UserBackups,
      })
      let query = {
        detail: event.detail.value,
        index: this.data.index
      }
      this.triggerEvent('myevent', query) //传值给父组件
    },
  }
})